home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / kernel / rtkernel.c < prev    next >
C/C++ Source or Header  |  1996-10-01  |  1KB  |  57 lines

  1.  
  2.  
  3. /*
  4.   Support for RTKernel 
  5.  
  6.   This module implements Windows 95/NT (Win32)
  7.   critical section facilities.
  8.  
  9.   Copyright 1996 Algorithms Corporation
  10. */
  11.  
  12. #include "dynrtk.h"
  13.  
  14.  
  15.  
  16. void    InitializeCriticalSection(CRITICAL_SECTION *cs)
  17. {
  18.     cs->s1 = RTKCreateSemaphore(Resource, 1, "NoName");
  19.     cs->s2 = RTKCreateSemaphore(Resource, 1, "NoName");
  20.     cs->t = (TaskHandle) 0;
  21.     cs->n = 0;
  22. }
  23.  
  24. void    DeleteCriticalSection(CRITICAL_SECTION *cs)
  25. {
  26.     RTKDeleteSemaphore(&cs->s1);
  27.     RTKDeleteSemaphore(&cs->s2);
  28. }
  29.  
  30. void    EnterCriticalSection(CRITICAL_SECTION *cs)
  31. {
  32.     RTKWait(cs->s1);
  33.     if (cs->t == RTKCurrentTaskHandle())
  34.         cs->n++;
  35.     else {
  36.         RTKSignal(cs->s1);
  37.         RTKWait(cs->s2);
  38.         RTKWait(cs->s1);
  39.         cs->n = 1;
  40.         cs->t = RTKCurrentTaskHandle();
  41.     }
  42.     RTKSignal(cs->s1);
  43. }
  44.  
  45. void    LeaveCriticalSection(CRITICAL_SECTION *cs)
  46. {
  47.     RTKWait(cs->s1);
  48.     if (cs->n  &&  cs->t == RTKCurrentTaskHandle())
  49.         if (!--cs->n) {
  50.             cs->t = (TaskHandle) 0;
  51.             RTKSignal(cs->s1);
  52.             RTKSignal(cs->s2);
  53.             return;
  54.         }
  55.     RTKSignal(cs->s1);
  56. }
  57.